using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using WozLib.WozCommands;
using WozLib.WozObjects;

namespace WOzTestUI.WozCommands
{
  public class WozAutoCommitOnCommand : WozObjectWozCommand
  {
    [XmlAttribute("WozCommitChangesCommand")]
    public WozCommitChangesCommand WozCommitChangesCommand
    {
      get
      {
        return _WozCommitChangesCommand;
      }
      set
      {
        _WozCommitChangesCommand = value;
      }
    } WozCommitChangesCommand _WozCommitChangesCommand;

    public WozAutoCommitOnCommand()
    {
    }

    public WozAutoCommitOnCommand(WizardWindow wizardWindow, WozCommitChangesCommand wozCommitChangesCommand)
      : base (wizardWindow)
    {
      WozCommitChangesCommand = wozCommitChangesCommand;
    }

    /// <summary>
    /// Returns the WizardWindow that is the target of this command. Returns null if none exists.
    /// </summary>
    [XmlIgnore]
    public WizardWindow TargetWizardWindow
    {
      get
      {
        IServerObject iServerObject = TargetIServerObject;
        if (iServerObject is WizardWindow)
        {
          return iServerObject as WizardWindow;
        }
        else
        {
          return null;
        }
      }
      set
      {
        TargetServerID = value.ServerID;
      }
    }

    public override void Undo()
    {
      if (WozCommitChangesCommand != null)
      {
        WozCommitChangesCommand.Undo();
      }

      WizardWindow wizardWindow = TargetWizardWindow;

      if (wizardWindow != null)
      {
        wizardWindow.AutoCommitOff(false);
      }
    }

    public override void Redo()
    {
      WizardWindow wizardWindow = TargetWizardWindow;
      if (wizardWindow != null)
      {
        wizardWindow.AutoCommitOn(false);
      }

      if (WozCommitChangesCommand != null)
      {
        WozCommitChangesCommand.Redo();
      }
    }
  }
}
